define(function(require){
	var $=require("jq");
	var Dialog=require("/pinzhiV2.0/js/pz_use_dialog.js");
	/**
	*@选择和输入数量小控件
	*@2014-03-04
	*@杨永
	*/
	function SelectNum(selectBox){
		var _this=this;
		//保存选择框
		this.selectBox=selectBox;
		//保存dim和add按钮
		this.dimBtn=selectBox.find(".dim");
		this.addBtn=selectBox.find(".add");
		//保存输入框
		this.input=selectBox.find("input");
		this.totalValue=parseInt(this.input.attr("data-total"));
		//保存提示框
		this.tipsBox=selectBox.find(".popup-tips")
		//绑定事件
		this.dimBtn.click(function(e){
			_this._dim(_this.input);
		});
		this.addBtn.click(function(e){
			_this._add(_this.input);
		});
		this.input.keyup(function(e){
			var val=Number($(this).val());
			if(isNaN(val)){
				$(this).val(_this.totalValue);
			};
			if(val>_this.totalValue){
				$(this).val(_this.totalValue);
			};
			if(val<=0){
				$(this).val(1);
			};
		});
		//阻止右键菜单
		this.input.get(0).oncontextmenu=function(){
			return false;
		};
	};
	SelectNum.prototype={
		//减贱
		_dim:function(input){
			var value=parseInt(input.val());
			value--;
			if(value<1){
				this._tips("最少提交1个");
			}else{
				input.val(value);
			};
		},
		_add:function(input){
			var value=parseInt(input.val());
			value++;
			if(value>this.totalValue){
				this._tips("最多提交"+this.totalValue+"个");
			}else{
				input.val(value);
			};
		},
		_tips:function(text){
			var _this=this;
			this.tipsBox.find("strong").text(text);
			this.tipsBox.addClass("popup-tips-show");
			window.setTimeout(function(){
				_this.tipsBox.removeClass("popup-tips-show");
			},2000);
		}
	};
	SelectNum.init=function(selectWraps){
		var _this=this;
		selectWraps.each(function(i,o){
			new _this($(this));
		});
	};
	//初始化所以对象
	if($("#after-service-wrapper .select-num-box").get(0)){
		SelectNum.init($("#after-service-wrapper .select-num-box"));
	};
		
	if($("#afterServiceContentType-2").get(0)){
		var showBox=$("#showBox");
		$("#afterServiceContentType-2").click(function(){
				showBox.show();
		});
		if($("#afterServiceContentType-2").get(0).checked){
			$("#afterServiceContentType-2").click();
		};
		$("#afterServiceContentType-1").click(function(){
			showBox.hide();
		});
	};
	//提交
	function J_Submit(subBox){
		//售后类型
		this.afterServiceTypeInput=$("input[name=afterServiceType]",subBox);
		//售后内容
		this.afterServiceContentTypeInput=$("input[name=afterServiceContentType]",subBox);
		//描述问题
		this.customerContent=$("textarea",subBox);
		//提交数量
		this.submitNum=$(".select-num-mod",subBox);
		//图片描述
		this.imgsList=$(".upload-wrapper img",subBox);
		//用户名
		this.userName=$("input[name=userName]",subBox);
		//手机号
		this.mobileNum=$("input[name=mobileNum]",subBox);
		//售后地址
		this.J_County=$("#J_County",subBox);
		this.detialAddress=$("input[name=detialAddress]",subBox);
		this.orderAliasCode=$("input[name=orderAliasCode]",subBox);
		//提交奥按钮
		this.submitUpload=$(".submitUpload",subBox);
		var _this_=this;
		this.submitUpload.click(function(){
			_this_.submit();
		});		
		
	};
	J_Submit.prototype={
			submit:function(){
				var _this_ = this;
				if(_this_.submitUpload.css("background")=="#ccc"){
					return ;
				}
				var data=this.getValue();
				if(data==''){
					return false;
				}
				if(data.afterServiceType!=4&&data.afterServiceType!=2){
					tipsText = "确认继续售后吗?";
				}else{
					tipsText = "当您的账户积分不足时，将从您的退款中扣除等价金额";
				}
				new Dialog({
					type:"confirm",
					  text:"提示",
					  tipsText:tipsText,
					  buttonSureText:"继续提交", 
					  buttonCancelText:"取消", 
					  height : 130,
					  mask:0.3,
					  callBack:function(){         
						  data = JSON.stringify(data);
						  $(this).css("background","#ccc");
							$.get("/erp/customerDocument/addFrontEndCustomerDocument.do",{data:data},function(json){
								if(json.msg=="ok"){
									window.location.href="/vipCenter/customerServiceList.do?menu_id=41";
								}else{
									new Dialog({
										type:"confirm",
										  text:"提示",
										  tipsText:json.reason,
										  buttonSureText:"知道了", 
										  buttonCancelText:"关闭", 
										  mask:0.3	
									});
									_this_.submitUpload.css("background","#EF363E")
								}
							},"json");       
					  },
					  cancelCallBack:function(){
							
					  }
				});
				
			},
			getValue:function(){
				var afterServiceType,afterServiceContentType,customerContent,userName,mobileNum,J_CountydetialAddress = '';
				this.afterServiceTypeInput.each(function(){
					if(this.checked){
						afterServiceType=this.value;
						return false;
					};
				});
				this.afterServiceContentTypeInput.each(function(){
					if(this.checked){
						afterServiceContentType=this.value;
						return false;
					};
				});
				var subNum=[];
				if(afterServiceContentType==0){
					this.submitNum.each(function(){
						var amount=$("input[name=amount]",this).val(),
							id=$("input[name=id]",this).val();
						if($("input[name=id]",this).get(0).checked){
							subNum.push({amount:amount,id:id});
						};
					});
					if(subNum==''){
						new Dialog({
							type:"confirm",
							  text:"提示",
							  tipsText:"必须选择商品信息",
							  buttonSureText:"知道了", 
							  buttonCancelText:"关闭", 
							  mask:0.3	
						});
						return '';
					}
				};
				customerContent=this.customerContent.val().trim();
				if(customerContent==''){
					new Dialog({
						type:"confirm",
						  text:"提示",
						  tipsText:"必须填写描述问题",
						  buttonSureText:"知道了", 
						  buttonCancelText:"关闭", 
						  mask:0.3	
					});
					return '';
				}
				var imgSrcs=[];
				this.imgsList.each(function(){
					imgSrcs.push($(this).attr("src"));
				});
				var flag = 0;
				for(var i=0;i<imgSrcs.length;i++){
					if (imgSrcs[i] != '' && typeof(imgSrcs[i])!="undefined"){
						flag = 1;
					}
				}
				if(flag==0){
					new Dialog({
						type:"confirm",
						  text:"提示",
						  tipsText:"至少上传一张图片",
						  buttonSureText:"知道了", 
						  buttonCancelText:"关闭", 
						  mask:0.3	
					});
					return '';
				}
				userName=this.userName.val().trim();
				if(userName==''){
					new Dialog({
						type:"confirm",
						  text:"提示",
						  tipsText:"必须填写姓名",
						  buttonSureText:"知道了", 
						  buttonCancelText:"关闭", 
						  mask:0.3	
					});
					return '';
				}
				mobileNum=this.mobileNum.val().trim();
				if(mobileNum==''){
					new Dialog({
						type:"confirm",
						  text:"提示",
						  tipsText:"必须填写手机号",
						  buttonSureText:"知道了", 
						  buttonCancelText:"关闭", 
						  mask:0.3	
					});
					return '';
				}else{
					var reg = /^0?1[3|4|5|8][0-9]\d{8}$/;
					 if (!reg.test(mobileNum)) {
						 new Dialog({
								type:"confirm",
								  text:"提示",
								  tipsText:"手机号不合法",
								  buttonSureText:"知道了", 
								  buttonCancelText:"关闭", 
								  mask:0.3	
							});
							return '';
					 }
				}
				J_County=this.J_County.val().trim();
				if(J_County==''){
					new Dialog({
						type:"confirm",
						  text:"提示",
						  tipsText:"必须填写完整地址",
						  buttonSureText:"知道了", 
						  buttonCancelText:"关闭", 
						  mask:0.3	
					});
					return '';
				}
				detialAddress=this.detialAddress.val().trim();
				if(detialAddress==''){
					new Dialog({
						type:"confirm",
						  text:"提示",
						  tipsText:"必须填写完整地址",
						  buttonSureText:"知道了", 
						  buttonCancelText:"关闭", 
						  mask:0.3	
					});
					return '';
				}
				var orderAliasCode = this.orderAliasCode.val();
				return {
					detialAddress:detialAddress,
					J_County:J_County,
					mobileNum:mobileNum,
					userName:userName,
					imgSrcs:imgSrcs,
					customerContent:customerContent,
					afterServiceContentType:afterServiceContentType,
					afterServiceType:afterServiceType,
					orderAliasCode:orderAliasCode,
					subNum:subNum
				}
				
			}
			
	};
	if($("#J_Submit").get(0)){
		var j_Submit=new J_Submit($("#J_Submit"));
	};
	

	
	
	
	
	
	if($("#flag").val() == 1){
		$("#afterServiceContentType-1").hide();
		$("#a1").hide();
		$("#afterServiceContentType-2").show();
		$("#afterServiceContentType-2").click();
	}
	
	$("#suc_confirm").on("click",function(){		
		var data = $("#docId").val();
		$.get("/erp/customerDocument/CustomerDocumentOver.do",{id:data},function(json){
			if(json.msg=="ok1"){
				window.location.href="/vipCenter/customerServiceList.do?menu_id=41";
			} else if (json.msg=="ok2"){
				window.location.href="/vipCenter/cancelServiceList.do?menu_id=41";
			}else{
				new Dialog({
					type:"confirm",
					  text:"失败",
					  tipsText:json.reason,
					  buttonSureText:"知道了", 
					  buttonCancelText:"关闭", 
					  mask:0.3	
				});
			}
		},"json");
		
	});
		
	if($("#canTH").val() == 0){
		$("#afterServiceType4").hide();
	}
	
	if($("#canHH").val() == 0){
		$("#afterServiceType3").hide();
	}
	
	
	var selectTypeBtn = $("#selectTypeBtn")
	
//	//写入退款HTML
//	selectTypeBtn.on("click",function(){		
//		var selectType = "<div class='J_PZLogin'id='select_type'><div class='J_PZLogin_box'id='validate-login'><div class='J_PZLogin_caption'><span class='J_PZLogin_caption_text'><a href='javascript:'class='close'>X</a>请选择</span></div><div class='J_PZLogin_content'><div class='select_type'><h3>您提交的售后订单，涉及到赠送或消费积分，请选择当您的积分余额不足时，退款处理方法：</h3><p class='select'><select id='choose_select'><option value='请等我三天内转回积分后退款'>请等我三天内转回积分后退款</option><option value='“积分不足时，用退款额进行抵销”'>“积分不足时，用退款额进行抵销”</option></select></p><p><a href='javascript:' class='submitUploadbtn'>确认</a></p><h4>说明</h4><p>1、”请等我三天内转回积分后退款“；当您积分余额不足时，品质365将等您三天时间补齐积分后，再为您办理退款，逾期我们将直接用退款金额抵销您不足的积分。</p><p>2、”积分不足时，用退款额进行抵销“；当您积分余额不足时，我们将直接用退款金额抵销您不足的积分。</p></div></div></div></div>"
//		innerDeliverFrom(selectType);
//
//	});
	//写入退款HTML
	selectTypeBtn.on("click",function(){
		//提交
		function J_Cancel(subBox){
			//售后原因
			this.afterServiceReasonInput=$("#afterServiceReason",subBox);
			//用户名
			this.userName=$("input[name=userName]",subBox);
			//手机号
			this.mobileNum=$("input[name=mobileNum]",subBox);
			this.orderAliasCode=$("input[name=orderAliasCode]",subBox);
			//提交奥按钮
			this.submitUpload=$(".submitUploadbtn",subBox);
			var _this_=this;
			this.submitUpload.click(function(){
				_this_.submit();
			});		
			
		};
		
		J_Cancel.prototype={
				submit:function(){
					var _this_ = this;
					if(_this_.submitUpload.css("background")=="#ccc"){
						return false;
					}
					var data=this.getValue();
					if(data==''){
						return false;
					}
					data = JSON.stringify(data);
					_this_.submitUpload.css("background","#ccc");
					$.get("/erp/customerDocument/addFrontEndCacelCustomerDocument.do",{data:data},function(json){
						if(json.msg=="ok"){
							window.location.href="/vipCenter/cancelServiceList.do?menu_id=41";
						}else{
							new Dialog({
								type:"confirm",
								  text:"失败",
								  tipsText:json.reason,
								  buttonSureText:"知道了", 
								  buttonCancelText:"关闭", 
								  mask:0.3	
							});
							_this_.submitUpload.css("background","#EF353D");
						}
					},"json");
				},
				getValue:function(){
					var afterServiceReason,userName,mobileNum;
					
					afterServiceReason=this.afterServiceReasonInput.val();
					userName=this.userName.val();
					mobileNum=this.mobileNum.val();
					var orderAliasCode = this.orderAliasCode.val();
					var backTypeDesc = $("#choose_select").val();
										
					return {afterServiceReason:afterServiceReason,userName:userName,mobileNum:mobileNum,orderAliasCode:orderAliasCode,backTypeDesc:backTypeDesc};				
				}
				
		};
		var j_Cancel=new J_Cancel($("#J_Cancel"));
		new Dialog({
			type:"confirm",
			  text:"提示",
			  tipsText:"当您的账户积分不足时，将从您的退款中扣除等价金额",
			  buttonSureText:"继续提交", 
			  buttonCancelText:"取消", 
			  height : 130,
			  mask:0.3,
			  callBack:function(){        
				  j_Cancel.submit();
			  },
			  cancelCallBack:function(){
					
			  }
		});
		
		
	});
	
	
//	//写入已发货html
//	var innerDeliverFrom = function(html){
//		var html = html;
//		var mask = "<div class='J_PZLogin_mask'></div>"
//		$("#J_Cancel").append(html +mask );
//		var J_PZLogin = $(".J_PZLogin");
//		var css = require('../../css/pz_login.css'); //引入css文件
//		$('.J_PZLogin_mask').css("height",$(window).height());
//		center(J_PZLogin);
//
//
//		//获取已收货按钮
//		var deliver_goodsOK = $("#deliver_goodsOK");
//		deliver_goodsOK.on("click",function(){
//
//			deliverGoodsOK();
//
//		});
//		
//		//提交
//		function J_Cancel(subBox){
//			//售后原因
//			this.afterServiceReasonInput=$("#afterServiceReason",subBox);
//			//用户名
//			this.userName=$("input[name=userName]",subBox);
//			//手机号
//			this.mobileNum=$("input[name=mobileNum]",subBox);
//			this.orderAliasCode=$("input[name=orderAliasCode]",subBox);
//			//提交奥按钮
//			this.submitUpload=$(".submitUploadbtn",subBox);
//			var _this_=this;
//			this.submitUpload.click(function(){
//				_this_.submit();
//			});		
//			
//		};
//		J_Cancel.prototype={
//				submit:function(){
//					var _this_ = this;
//					if(_this_.submitUpload.css("background")=="#ccc"){
//						return false;
//					}
//					var data=this.getValue();
//					if(data==''){
//						return false;
//					}
//					data = JSON.stringify(data);
//					_this_.submitUpload.css("background","#ccc");
//					$.get("/erp/customerDocument/addFrontEndCacelCustomerDocument.do",{data:data},function(json){
//						if(json.msg=="ok"){
//							window.location.href="/vipCenter/cancelServiceList.do?menu_id=41";
//						}else{
//							new Dialog({
//								type:"confirm",
//								  text:"失败",
//								  tipsText:json.reason,
//								  buttonSureText:"知道了", 
//								  buttonCancelText:"关闭", 
//								  mask:0.3	
//							});
//							_this_.submitUpload.css("background","#EF353D");
//						}
//					},"json");
//				},
//				getValue:function(){
//					var afterServiceReason,userName,mobileNum;
//					
//					afterServiceReason=this.afterServiceReasonInput.val();
//					userName=this.userName.val();
//					mobileNum=this.mobileNum.val();
//					var orderAliasCode = this.orderAliasCode.val();
//					var backTypeDesc = $("#choose_select").val();
//										
//					return {afterServiceReason:afterServiceReason,userName:userName,mobileNum:mobileNum,orderAliasCode:orderAliasCode,backTypeDesc:backTypeDesc};				
//				}
//				
//		};
//		var j_Cancel=new J_Cancel($("#J_Cancel"));
//		
//		
//		//获取退款按钮
//		var selectOk = $("#selectOk");
//
//		$(".close").on("click",function(){
//
//			closeLayer();
//		});
//
//	}
	
	var center = function(dom){
	
		 	var top = ($(window).height() - dom.height())/2;
	         var left = ($(window).width() - dom.width())/2;
	         var scrollTop = $(document).scrollTop();
	         var scrollLeft = $(document).scrollLeft();
	
	
	         if (top<0) {
	          var top = 30;
	         }
	         dom.css( { position : 'absolute', 'top' : top+scrollTop, left : left + scrollLeft } );
	}
	
	//关闭弹出层
	var closeLayer = function(){

		$(".J_PZLogin").remove();
		$('.J_PZLogin_mask').remove();


	}

});